package cn.edu.imust.loginregisterdemo.login.view;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.edu.imust.loginregisterdemo.R;
import cn.edu.imust.loginregisterdemo.TimeCount;
import cn.edu.imust.loginregisterdemo.base.BaseFragment;
import cn.edu.imust.loginregisterdemo.home.view.MainActivity;
import cn.edu.imust.loginregisterdemo.utils.AccountValidatorUtil;
import cn.edu.imust.loginregisterdemo.utils.Utils;

public class SmsLoginFragment extends BaseFragment {

    @BindView(R.id.et_phone)
            EditText etPhone;
    @BindView(R.id.sms_content)
            EditText smsContent;
    @BindView(R.id.get_sms)
            TextView   getSms;
    @BindView(R.id.tv_termOfService)
            TextView tvTermOfService;
    @BindView(R.id.s_login)
            Button   sLogin;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sms_login, container, false);
        Bmob.initialize(getContext(), "33452dfaa0b4f1b9b1a5ea2c1818d5f4");
        ButterKnife.bind(this, view);
        return view;
    }

    @OnClick({R.id.get_sms, R.id.tv_termOfService, R.id.s_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.get_sms:
                String currentPhoneNum = etPhone.getText().toString().trim();
                if (TextUtils.isEmpty(currentPhoneNum)) {
                    Utils.showSnackBar(getContext(), view, "手机号不能为空");
                }else if (!AccountValidatorUtil.isMobile(currentPhoneNum)) {
                    Utils.showSnackBar(getContext(), view, "手机号不正确");
                } else {
                    TimeCount timeCount = new TimeCount(getSms, 60 * 1000, 1000);
                    timeCount.start();
                    //TODO  手机验证码登录
                    /**
                     * TODO template 如果是自定义短信模板，此处替换为你在控制台设置的自定义短信模板名称；如果没有对应的自定义短信模板，则使用默认短信模板。
                     */
                    BmobSMS.requestSMSCode(currentPhoneNum, "牧场记录", new QueryListener<Integer>() {
                        @Override
                        public void done(Integer smsId, BmobException e) {
                            if (e == null) {
                                Utils.showSnackBar(getContext(), view, "发送验证码成功，短信ID：" + smsId);
                            } else {
                                Utils.showSnackBar(getContext(), view, "发送验证码失败-" + e.getMessage());
                            }
                        }
                    });
                }
                break;
            case R.id.tv_termOfService:
                Toast.makeText(getContext(), "服务条款", Toast.LENGTH_SHORT).show();
                break;
            case R.id.s_login:
                //验证码登录逻辑
                String currentPhoneNum2 = etPhone.getText().toString().trim();
                String code = smsContent.getText().toString().trim();
                if (currentPhoneNum2.length() == 0 || code.length() == 0) {
                    Utils.showSnackBar(getContext(), view, "手机号或验证码不能为空");
                } else if (!AccountValidatorUtil.isMobile(currentPhoneNum2)) {
                    Utils.showSnackBar(getContext(), view, "手机号格式错误");
                } else {
                    BmobSMS.verifySmsCode(currentPhoneNum2, code, new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if (e == null) {
                                Toast.makeText(getActivity(), "验证码验证成功，您可以在此时进行绑定操作！", Toast.LENGTH_SHORT).show();
                                /**
                                 * TODO 此API需要在用户已经注册并验证的前提下才能使用
                                 */
                                BmobUser.loginBySMSCode(currentPhoneNum2, code, new LogInListener<BmobUser>() {
                                    @Override
                                    public void done(BmobUser bmobUser, BmobException e) {
                                        if (e == null) {
                                            Utils.showSnackBar(getContext(), view, "短信登录成功：" + bmobUser.getObjectId()
                                                    +"-"+bmobUser.getUsername());
                                            startActivity(new Intent(getContext(), MainActivity.class));
                                        } else {
                                            Utils.showSnackBar(getContext(), view, "短信登录失败：");
                                        }
                                    }
                                });
                            } else {
                                Utils.showSnackBar(getContext(), view, "验证码验证失败：" + e.getErrorCode()
                                                + "-" + e.getMessage());
                                }
                        }
                    });
                }
                break;
        }
    }


}

